/// picker_page.dart
///
/// Created by fuchuanwei on Wed Mar 15 2023.
///
/// Description: 各种类型的选择器
///
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:my_app/app/widgets/custom_title_widget.dart';
import 'package:my_app/app/widgets/image_preview_gallery_widget.dart';
import 'package:my_app/app/widgets/picker/custom_default_picker_widget.dart';
import 'package:my_app/app/widgets/picker/custom_image_picker_widget.dart';

class PickerPage extends StatelessWidget {
  static String pickerData = '''
[
    {
      "中国":["北京","上海","山东"]
    },
    {
      "美国":["洛杉矶","芝加哥","波士顿"]
    },
    {
      "日本":["东京","大阪","神户"]
    }
    
]
    ''';

  final CustomImagePickerWidgetController controller =
      CustomImagePickerWidgetController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('选择器')),
      body: ListView(
        padding: EdgeInsets.all(10.0),
        children: [
          ElevatedButton(
            onPressed: () {
              Picker picker = CustomDefaultPickerWidget(
                adapter: PickerDataAdapter<String>(
                    pickerData: JsonDecoder().convert(pickerData)),
                titleText: "选择国家",
                onConfirm: (Picker picker, List value) {
                  print(value.toString());
                  print(picker.getSelectedValues());
                },
              );
              picker.showModal(context);
            },
            child: Text('基础使用(flutter_picker)'),
          ),
          ElevatedButton(
            onPressed: () {
              showTimePicker(context: context, initialTime: TimeOfDay.now());
            },
            child: Text('时间选择器(flutter自带)'),
          ),
          ElevatedButton(
            onPressed: () {
              showDatePicker(
                  context: context,
                  initialDate: DateTime.now(),
                  firstDate: DateTime(2000),
                  lastDate: DateTime(2030));
            },
            child: Text('日期选择器(flutter自带)'),
          ),
          ElevatedButton(
            onPressed: () {
              showDateRangePicker(
                  context: context,
                  firstDate: DateTime(2000),
                  lastDate: DateTime(2030));
            },
            child: Text('时间范围选择器(flutter自带)'),
          ),
          ElevatedButton(
            onPressed: () {
              DatePicker.showDatePicker(
                context,
                minDateTime: DateTime(2023),
                maxDateTime: DateTime(2025),
                initialDateTime: DateTime.now(),
                dateFormat: "yyyy年 MM月 dd日 HH时 mm分",
                locale: DateTimePickerLocale.zh_cn,
                onChange: (DateTime time, indexs) {},
                onConfirm: (DateTime time, indexs) {},
              );
            },
            child: Text('日期时间选择器(ios风格)'),
          ),
          SizedBox(height: 30),
          imagePickerDemo(),
        ],
      ),
    );
  }

  Widget imagePickerDemo() {
    List<ImageGalleryEntity> list = [
      ImageGalleryEntity.fromJson({
        "url":
            "https://img0.baidu.com/it/u=1319729121,2438624844&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=695",
        "tag": "1319729121"
      }),
      ImageGalleryEntity.fromJson({
        "url":
            "https://img2.baidu.com/it/u=3181075760,2628856001&fm=253&fmt=auto&app=138&f=JPEG?w=658&h=339",
        "tag": "3181075760"
      })
    ];

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        CustomTitleWidget(title: '图片选择器'),
        SizedBox(
          height: 10.0,
        ),
        CustomImagePickerWidget(
          imageEntitys: list,
          controller: controller,
        ),
        SizedBox(
          height: 10.0,
        ),
        SizedBox(
          width: double.infinity,
          child: ElevatedButton(
            onPressed: () {
              controller.clear();
            },
            child: Text('清空所有图片'),
          ),
        ),
        SizedBox(
          width: double.infinity,
          child: ElevatedButton(
            onPressed: () async {
              var list = await controller.getSelectedImages();
              if (list.isNotEmpty) {
                print(list[0]);
                print(list[1]);
              }
            },
            child: Text('获取选择的图片列表'),
          ),
        ),
      ],
    );
  }
}
